package com.tadpole.music.presenter.question;

import com.tadpole.music.bean.question.SimulationBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.question.SimulationIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟套题列表
 * Created By 孙岩
 * on 2020/9/18
 */
public class SimulationPresenter extends BasePresenter<SimulationIView> {
    private int page = 2;
    private List<SimulationBean.DataBeanX.DataBean> list;

    /**
     * @param college_id 学院id
     * @param rank_id    等级id
     */
    @SuppressWarnings("unchecked")
    public void getSimulation(String college_id, String rank_id, final boolean isRefresh) {
        String url = UrlConfig.getSimulationQuestions + "?page=1&college_id=" + college_id + "&rank_id=" + rank_id;
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .url(url)
                .token(token)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                list = new ArrayList<>();
                                JSONObject object = new JSONObject(obj.getString("data"));
                                JSONArray array = new JSONArray(object.getString("data"));
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                    SimulationBean.DataBeanX.DataBean bean = new SimulationBean.DataBeanX.DataBean(
                                            JsonParseUtil.getInt(jsonObject, "id"),
                                            JsonParseUtil.getStr(jsonObject, "created_at"),
                                            JsonParseUtil.getStr(jsonObject, "updated_at"),
                                            JsonParseUtil.getInt(jsonObject, "college_id"),
                                            JsonParseUtil.getInt(jsonObject, "rank_id"),
                                            JsonParseUtil.getStr(jsonObject, "desc"),
                                            JsonParseUtil.getStr(jsonObject, "name"),
                                            JsonParseUtil.getInt(jsonObject, "status"),
                                            JsonParseUtil.getBoolean(jsonObject, "paid")
                                    );
                                    list.add(bean);
                                }

                                if (isRefresh) {
                                    page = 2;
                                    getView().showSimulationQuestion(list);
                                    getView().notifyAdapter();
                                } else {
                                    getView().showSimulationQuestion(list);
                                }
                            } else if (obj.getInt("code") == 202) {
                                getView().showNone();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onComplete() {
                        getView().stopRefresh();
                    }

                    @Override
                    public void onBefore() {

                    }
                });
    }

    @SuppressWarnings("unchecked")
    public void getSimulationQuestionMore(String college_id, String rank_id) {
        String url = UrlConfig.getSimulationQuestions + "?page=" + page + "&college_id=" + college_id + "&rank_id=" + rank_id;
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .url(url)
                .token(token)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                List<SimulationBean.DataBeanX.DataBean> newList = new ArrayList<>();
                                JSONObject object = new JSONObject(obj.getString("data"));
                                JSONArray array = new JSONArray(object.getString("data"));
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                    SimulationBean.DataBeanX.DataBean bean = new SimulationBean.DataBeanX.DataBean(
                                            JsonParseUtil.getInt(jsonObject, "id"),
                                            JsonParseUtil.getStr(jsonObject, "created_at"),
                                            JsonParseUtil.getStr(jsonObject, "updated_at"),
                                            JsonParseUtil.getInt(jsonObject, "college_id"),
                                            JsonParseUtil.getInt(jsonObject, "rank_id"),
                                            JsonParseUtil.getStr(jsonObject, "desc"),
                                            JsonParseUtil.getStr(jsonObject, "name"),
                                            JsonParseUtil.getInt(jsonObject, "status"),
                                            JsonParseUtil.getBoolean(jsonObject, "paid")
                                    );
                                    newList.add(bean);
                                }
                                list.addAll(newList);
                                page++;
                                getView().notifyAdapter();
                            } else if (obj.getInt("code") == 202) {
                                getView().showNone();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                        getView().stopRefresh();

                    }

                    @Override
                    public void onComplete() {
                        getView().stopRefresh();
                    }

                    @Override
                    public void onBefore() {

                    }
                });
    }
}

